//
//  TLST_DetailVC.m
//  Travel_World
//
//  Created by lanou on 15/10/13.
//  Copyright © 2015年 TLS. All rights reserved.
//

#import "TLST_DetailVC.h"
#import "TLST_DetailModel.h"
#import "TLST_DetailFrameModel.h"
#import "TLST_DetailCell.h"
#import "QTAFNnetWorkTool.h"
#import "TLSWebViewController.h"
#import "TLS_TcoumnityModel.h"
#import "UIImageView+WebCache.h"

@interface TLST_DetailVC ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
@property (nonatomic, strong) UITableView *detailTableView;
@property (nonatomic, strong) NSMutableArray *detailFrameModelArr;
@property (nonatomic, strong) NSMutableArray *segArr;
@property (nonatomic, strong) NSMutableArray *typeArr;
@property (nonatomic, strong) UISegmentedControl *segVC;
@property (nonatomic, strong) UIScrollView *bigScrollView;
@property (nonatomic, strong) NSMutableArray *urlStrArr;
@property (nonatomic, strong) UIScrollView *titleScrollView;
@property (nonatomic, strong) UILabel *lineLabel;

@property (nonatomic, strong) UITableView *allTableView;
@property (nonatomic, strong) NSMutableArray *allFrameModelArr;
@property (nonatomic, strong) UITableView *currentTableView;
@property (nonatomic, strong) NSMutableArray *currentFrameModelArr;
@property (nonatomic, strong) UITableView *bestTableView;
@property (nonatomic, strong) NSMutableArray *bestFrameModelArr;
@end

@implementation TLST_DetailVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.allFrameModelArr = [NSMutableArray array];
        self.currentFrameModelArr = [NSMutableArray array];
        self.bestFrameModelArr = [NSMutableArray array];
        self.detailFrameModelArr = [NSMutableArray array];
        self.typeArr = [NSMutableArray array];
        self.segArr = [NSMutableArray arrayWithObjects:@"全部",@"最新",@"精华", nil];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.tcoumityModel.name;
    [self createUrlStr];
    for (NSDictionary *tempDic in self.tcoumityModel.types) {
        [self.segArr addObject:tempDic[@"name"]];
        [self.typeArr addObject:tempDic[@"id"]];
    }
    [self createView];
}
- (void)createView
{
    UIView *headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, WIDTH, 40);
    headerView.backgroundColor = [UIColor colorWithWhite:0.907 alpha:1.000];
    [self.view addSubview:headerView];
    
    self.titleScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    self.titleScrollView.contentSize = CGSizeMake(80*self.segArr.count, 30);
    [headerView addSubview:self.titleScrollView];
    
    self.segVC = [[UISegmentedControl alloc] initWithItems:self.segArr];
    self.segVC.frame = CGRectMake(0, 0, 80*self.segArr.count, 40);
    self.segVC.selectedSegmentIndex = 0;
    [self.titleScrollView addSubview:self.segVC];
    [self.segVC addTarget:self action:@selector(selectTableView) forControlEvents:UIControlEventValueChanged];
    self.segVC.tintColor = [UIColor clearColor];//选中背景颜色
    NSDictionary *selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor colorWithRed:0.050 green:0.404 blue:0.820 alpha:1.000]};
    [self.segVC setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16], NSForegroundColorAttributeName: [UIColor colorWithWhite:0.145 alpha:1.000]};
    [self.segVC setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    
    // 标题下面的细线
    self.lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 38, 80, 2)];
    [self.segVC addSubview:_lineLabel];
    self.lineLabel.backgroundColor = [UIColor colorWithRed:0.050 green:0.404 blue:0.820 alpha:1.000];
    
    self.bigScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, WIDTH, HEIGHT-40)];
    self.bigScrollView.contentSize = CGSizeMake(WIDTH*self.segArr.count, HEIGHT-40);
    self.bigScrollView.delegate = self;
    self.bigScrollView.pagingEnabled = YES;
    self.bigScrollView.bounces = NO;
    self.bigScrollView.directionalLockEnabled = YES;
    self.bigScrollView.showsHorizontalScrollIndicator = NO;
    self.bigScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.bigScrollView];
    
    // 前面三个公有tableView
    self.allTableView = [self createTableViewWithTag:0 frameModelArr:_allFrameModelArr];
    self.currentTableView = [self createTableViewWithTag:1 frameModelArr:_currentFrameModelArr];
    self.bestTableView = [self createTableViewWithTag:2 frameModelArr:_bestFrameModelArr];
    // 后面的tableView
    for (NSInteger i = 0; i < self.segArr.count-3; i++) {
        [self createTableViewWithType:[self.typeArr[i] integerValue] andTag:i+3];
    }
}
- (void)selectTableView{
    self.bigScrollView.contentOffset = CGPointMake(self.segVC.selectedSegmentIndex*WIDTH, 0);
    self.lineLabel.frame = CGRectMake(self.segVC.selectedSegmentIndex*80, 38, 80, 2);
}
// 创建前三个tableView
- (UITableView *)createTableViewWithTag:(NSInteger)tag frameModelArr:(NSMutableArray *)frameModelArr {
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH*tag, 0, WIDTH, HEIGHT-40) style:UITableViewStylePlain];
    tableView.tag = 101+tag;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.bigScrollView addSubview:tableView];
    [self createDataWithTableView:tableView frameModelArr:frameModelArr urlStr:_urlStrArr[tag]];
    tableView.tableFooterView = [[UIView alloc] init];
    [self.bigScrollView addSubview:tableView];
    return tableView;
}
// 创建后面的tableView
- (UITableView *)createTableViewWithType:(NSInteger)type andTag:(NSInteger)tag{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH*tag, 0, WIDTH, HEIGHT-40) style:UITableViewStylePlain];
    tableView.tag = 101+tag;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self createDataWithType:type andTag:tag];
    tableView.tableFooterView = [[UIView alloc] init];
    [self.bigScrollView addSubview:tableView];
    return tableView;
}
// 前面三个tableView数据请求
//- (void)createDataWithStr:(NSString *)str andTag:(NSInteger)tag
- (void)createDataWithTableView:(UITableView *)tableView frameModelArr:(NSMutableArray *)frameModelArr urlStr:(NSString *)urlStr
{
      [QTAFNnetWorkTool getUrl:urlStr body:nil hudSuperView:self.view result:QTJSON headerFile:nil success:^(id result) {
        NSMutableArray *detModArr = [TLST_DetailModel baseModelByArr:result[@"data"][@"entry"]];
        NSLog(@"%ld",self.myId);
          
        for (TLST_DetailModel *tempModel in detModArr) {
            TLST_DetailFrameModel *frameModel = [[TLST_DetailFrameModel alloc] init];
            frameModel.detailModel = tempModel;
            [frameModelArr addObject:frameModel];
        }
        self.placeLabel.hidden = YES;
        [tableView reloadData];
    }];
}
// 1.游记 2.攻略  3.结伴  4.转让  5.  6.讨论  7.退税  8.主题活动   9.境外购物   10.境外用卡  12.参赛计划
- (void)createDataWithType:(NSInteger)type andTag:(NSInteger)tag
{
    NSString *str = [NSString stringWithFormat:@"http://open.qyer.com/qyer/bbs/forum_thread_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&delcache=1&forum_id=%ld&forum_type=%ld&lat=38.88261036070509&lon=121.5397574228773&oauth_token=55d7f8a1931dedc7d5a8f24ab63426d0&page=1&track_app_channel=App%%2520Store&track_app_version=6.7&track_device_info=iPhone5%%2C4&track_deviceid=41AE760B-510C-4778-AF4D-177B387E0F3B&track_os=ios%%25209.0.2&track_user_id=6852801&type=all&v=1",self.myId,type];
    [QTAFNnetWorkTool getUrl:str body:nil hudSuperView:self.placeLabel result:QTJSON headerFile:nil success:^(id result) {
        NSMutableArray *detModArr = [TLST_DetailModel baseModelByArr:result[@"data"][@"entry"]];
         if (self.detailFrameModelArr.count > 0) {
            [self.detailFrameModelArr removeAllObjects];
        }
        for (TLST_DetailModel *tempModel in detModArr) {
            TLST_DetailFrameModel *frameModel = [[TLST_DetailFrameModel alloc] init];
            frameModel.detailModel = tempModel;
            [self.detailFrameModelArr addObject:frameModel];
        }
        self.placeLabel.hidden = YES;
        UITableView *tableView = (UITableView *)[self.view viewWithTag:101+tag];
        [tableView reloadData];
        //[self.detailTableView reloadData];
    }];
}
#pragma mark tableView 数据源,代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.allTableView) {
        return self.allFrameModelArr.count;
    } else if (tableView == self.currentTableView) {
        return self.currentFrameModelArr.count;
    } else if (tableView == self.bestTableView) {
        return self.bestFrameModelArr.count;
    } else {
    return self.detailFrameModelArr.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TLST_DetailCell *cell = [TLST_DetailCell tcellWithTableView:tableView];
    
    if (tableView == self.allTableView && self.allFrameModelArr.count > indexPath.row){
        cell.detailFrameModel = self.allFrameModelArr[indexPath.row];
    }
    else if (tableView == self.currentTableView && self.currentFrameModelArr.count > indexPath.row) {
        cell.detailFrameModel = self.currentFrameModelArr[indexPath.row];
    }
    else if (tableView == self.bestTableView && self.bestFrameModelArr.count > indexPath.row) {
        cell.detailFrameModel = self.bestFrameModelArr[indexPath.row];
    }
    else {
        cell.detailFrameModel = self.detailFrameModelArr[indexPath.row];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.allTableView && self.allFrameModelArr.count > indexPath.row) {
        TLST_DetailFrameModel *model = self.allFrameModelArr[indexPath.row];
        return model.cellHeight;
    } else if (tableView == self.currentTableView && self.currentFrameModelArr.count > indexPath.row) {
        TLST_DetailFrameModel *model = self.currentFrameModelArr[indexPath.row];
        return model.cellHeight;
    } else if (tableView == self.bestTableView && self.bestFrameModelArr.count > indexPath.row) {
        TLST_DetailFrameModel *model = self.bestFrameModelArr[indexPath.row];
        return model.cellHeight;
    } else {
        TLST_DetailFrameModel *model = self.detailFrameModelArr[indexPath.row];
        return model.cellHeight;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TLSWebViewController *webViewControl = [[TLSWebViewController alloc] init];
    
    if (tableView == self.allTableView && self.allFrameModelArr.count > indexPath.row) {
        TLST_DetailFrameModel *model = self.allFrameModelArr[indexPath.row];
        webViewControl.urlStr = model.detailModel.view_url;

    } else if (tableView == self.currentTableView && self.currentFrameModelArr.count > indexPath.row) {
        TLST_DetailFrameModel *model = self.currentFrameModelArr[indexPath.row];
        webViewControl.urlStr = model.detailModel.view_url;
        
    } else if (tableView == self.bestTableView && self.bestFrameModelArr.count > indexPath.row) {
        TLST_DetailFrameModel *model = self.bestFrameModelArr[indexPath.row];
        webViewControl.urlStr = model.detailModel.view_url;

    } else {
        TLST_DetailFrameModel *model = self.detailFrameModelArr[indexPath.row];
        webViewControl.urlStr = model.detailModel.view_url;
    }
    
    [self.navigationController pushViewController:webViewControl animated:YES];
}
#pragma mark 代理
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == self.bigScrollView) {
        self.segVC.selectedSegmentIndex = self.bigScrollView.contentOffset.x/WIDTH;
        self.lineLabel.frame = CGRectMake(self.segVC.selectedSegmentIndex*80, 38, 80, 2);
        
        if (self.segVC.selectedSegmentIndex > 0 && self.segVC.selectedSegmentIndex < self.segArr.count-1 ) {
            NSInteger index = self.segVC.selectedSegmentIndex - 1;
            self.titleScrollView.contentOffset = CGPointMake(index * 85, 0);
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}
- (void)createUrlStr
{
    NSString *allUrl = [NSString stringWithFormat:@"http://open.qyer.com/qyer/bbs/forum_thread_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&delcache=1&forum_id=%ld&lat=38.88261036070509&lon=121.5397574228773&oauth_token=55d7f8a1931dedc7d5a8f24ab63426d0&page=1&track_app_channel=App%%2520Store&track_app_version=6.7&track_device_info=iPhone5%%2C4&track_deviceid=41AE760B-510C-4778-AF4D-177B387E0F3B&track_os=ios%%25209.0.2&track_user_id=6852801&v=1",self.myId];
    NSString *newUrl = [NSString stringWithFormat:@"http://open.qyer.com/qyer/bbs/forum_thread_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&delcache=1&forum_id=%ld&lat=38.88262860611146&lon=121.5396766802357&page=1&track_app_channel=App%%2520Store&track_app_version=6.5.5&track_device_info=iPhone7%%2C2&track_deviceid=5B302CA5-E537-4B10-955D-520F4C91BAFE&track_os=ios%%25208.1.2&type=new&v=1%%20",self.myId];
    NSString *bestUrl = [NSString stringWithFormat:@"http://open.qyer.com/qyer/bbs/forum_thread_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&delcache=1&forum_id=%ld&lat=38.88261036070509&lon=121.5397574228773&oauth_token=55d7f8a1931dedc7d5a8f24ab63426d0&page=1&track_app_channel=App%%2520Store&track_app_version=6.7&track_device_info=iPhone5%%2C4&track_deviceid=41AE760B-510C-4778-AF4D-177B387E0F3B&track_os=ios%%25209.0.2&track_user_id=6852801&type=digest&v=1",self.myId];
    self.urlStrArr = [NSMutableArray arrayWithObjects:allUrl,newUrl,bestUrl, nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
